/**
 * @(#) PaymentProxy.as
 *
 * @date		2010. 11. 01
 * @version		2.0
 * @author		Shin, Yunwoo
 * @client		Service Ace 
 * @module		Compensation  (급여)
 * @description	급여 상세  내역 조회- Proxy
 * 				
 */
package com.skt.common_hr.compensation.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.utilSilver.textSilver;
	import com.skt.common_hr.compensation.CompensationFacade;
	import com.skt.common_hr.compensation.model.vo.PayDetailVO;
	import com.skt.common_hr.compensation.model.vo.PayMasterVO;
	import com.skt.common_hr.compensation.model.vo.PaySummary;
	import com.skt.common_hr.compensation.model.vo.noticePayVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.NumberUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class PaymentProxy extends CommonProxy
	{
		public static const NAME:String = "PaymentProxy";
		
		public var payment:PayMasterVO;
		public var paysum:PaySummary;
		public var payTitle:String;
		
		public var acPopDetail:ArrayCollection;
		
		
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function PaymentProxy(data:Object=null)
		{
			super(NAME, data);
			
		}
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(payVO:PaySummary):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = Variables.myPernr;						// 사번
			param["I_OCRSN"] = payVO.OCRSN;					// 지급 사유 코드
			param["I_PAYDT"] = payVO.PAYDT_F;

			if(Variables.company == 'SAC') {
				param["I_PAYTY"] = payVO.PAYTY;		
				param["I_PAYID"] = payVO.PAYID;
			}
			loadData("Z_HR_0013", param, loadResult, loadFault);
		}


		//20131120
		// 렌탈 리스트 호출
		public function rentalList(setFunc:Function):void
		{
			var obj:Object = {};

			obj.I_PERNR		= Variables.myPernr;
			loadData("Z_HR_D001", obj, rentalListResult, listFault, setFunc);
		}
		public function rentalListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var mainList:ArrayCollection = parseMainList2(event.result.OTAB);//OTAB1
				var cbList:ArrayCollection = parseMainList2(event.result.OTAB1);//OTAB1
				
				var obj:Object = {};
				obj.mainList = mainList;
				obj.cbList = cbList;
				event.result.func(obj);
			}
			else {
				Alert.show(retText);
			}
		}	

		private function parseMainList2(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var obj:Object in recArr){
				//var retObj:MedicalSubTabVO = new MedicalSubTabVO();			
				var retObj:Object = new Object();
				for(var field:String in obj) {					
					if(field == "PAYDT" || field == "DATBW" || field == "DLEND" ) {					
						retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DT']);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DT']);
						retObj[field] = obj[field];
					}
					else if(field == "DARBT" || field == "REBAL" || field == "REPAY" || field == "RMPAY"  ) {
						retObj[field] = Math.round(obj[field]*100);
						retObj[field+'_DF'] = NumberUtil.NumberFormat(Math.round(obj[field]*100));
					}
					else {
						retObj[field] = obj[field];
					}					
				} 
				retArr.addItem(retObj);				
			} 
			return retArr;
		}







		// 공지사항 리스트 호출
		public function loadList(BEGDA:Date, ENDDA:Date, SEARCH:String, setFunc:Function):void
		{
			var obj:Object = {};

			obj.I_BEGDA		= BEGDA;
			obj.I_ENDDA		= ENDDA;
			obj.I_SEARCH	= SEARCH;

			obj = textSilver.toJsonType(obj);

			loadData("Z_HR_PC01", obj, loadListResult, listFault, setFunc);
		}
		public function loadListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var mainList:ArrayCollection = parseMainList(event.result.TBL_BOARD);
				event.result.func(mainList);
			}
			else {
				Alert.show(retText);
			}
		}		

		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}
				

		private function parseMainList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var obj:Object in recArr){
				var retObj:noticePayVO = new noticePayVO();  

				for(var field:String in obj) {
					if(retObj.hasOwnProperty(field)==false ) {
						continue;
					}
									
					if(field == "CREATE_DATE" ) {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field]);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field]);
					}											
					else {						
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function loadResult(event:ResultEvent):void
		{
			if(retCode==0) { 
				payTitle = StringUtil.NVL(event.result.E_PAYTEXT); 
				payment = parsePayMaster(payment, event.result.T_RESULT1);
				parsePayDetail(payment, event.result.T_RESULT2);
				
				// 21030311
				acPopDetail = parsePopDetail(event.result.T_RESULT3);
				
				facade.sendNotification(CompensationFacade.LOAD_PAYDETAIL_COMPLETE, this);
				
			}
			else {
				facade.sendNotification(CompensationFacade.LOAD_PAYDETAIL_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function loadFault(event:FaultEvent):void
		{
			facade.sendNotification(CompensationFacade.LOAD_PAYDETAIL_FAILED, this);
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parsePayMaster(vo:PayMasterVO, table:ArrayCollection):PayMasterVO
		{
			var rec:Object = table.getItemAt(0);
			vo = new PayMasterVO();
			
			vo.PERNR 	= rec.PERNR;
			vo.ENAME	= rec.ENAME;
			vo.ORGEH	= rec.ORGEH;
			vo.STEXT	= rec.STEXT;
			vo.BANKN	= rec.BANKN;
			vo.BANKA	= rec.BANKA;
			vo.WTTIME	= rec.WTTIME;
			vo.WOTIME	= rec.WOTIME;
			vo.HOTIME	= rec.HOTIME;
			vo.HNTIME	= rec.HNTIME;
			return vo;
		}



		// VO 상세 가족조회
		private function parsePopDetail(recArr:ArrayCollection):ArrayCollection//hthDetailFamilyVO
		{
			var retArr:ArrayCollection = new ArrayCollection();
			

			for each(var obj:Object in recArr){
				var retObj:Object = {};
				for(var field:String in obj) {
					
					// 항목이 비어있지 않다면 
					if(field == "LTEXT") {
						retObj[field] = obj[field];
						//retObj[field] = Math.round(obj[field]*100);
					}
					else if(field == "BETRG") {
						if(obj[field] != "0") {
							retObj[field] = obj[field];
						} else {
							retObj[field] = "";
						}
						
					}	
					else if(field == "ZTEXT"){
						retObj[field] = obj[field];
					}	
					
									
				
				}
				retArr.addItem(retObj);				
			}
			return retArr;
		}
		
		
		private function parsePayDetail(vo:PayMasterVO, table:ArrayCollection):void
		{
			vo.payDetail 	= new Array();
			vo.subtDetail 	= new Array();
			
			for each(var rec:Object in table) {
				var detailVO:PayDetailVO = new PayDetailVO();
				switch(rec.ITMNO.toString()) {
					case '001':		// 지급 총액
						vo.paySum = Math.round(rec.BETRG*100);
						detailVO.ITMNO = rec.ITMNO;
						detailVO.ITMNM = rec.ITMNM;
						detailVO.BETRG = Math.round(rec.BETRG*100);
						//vo.payDetail.push(detailVO);
						break;
					case '002':		// 공제 총액
						vo.subtSum = Math.round(rec.BETRG*100);
						detailVO.ITMNM = rec.ITMNM;
						detailVO.BETRG = Math.round(rec.BETRG*100);
						//vo.payDetail.push(detailVO);
						break;
					case '003':		// 지급액
						vo.receivedSum = Math.round(rec.BETRG*100);
						detailVO.ITMNM = rec.ITMNM;
						detailVO.BETRG = Math.round(rec.BETRG*100);
						//vo.payDetail.push(detailVO);
						break;
					case "038":		// 2010.11.2 선지급금 화면 상단으로..
						vo.prereceived = Math.round(rec.BETRG*100);
						detailVO.ITMNM = rec.ITMNM;
						detailVO.BETRG = Math.round(rec.BETRG*100);
						break;												
					default:
					/*
						var detailVO:PayDetailVO = new PayDetailVO();
						detailVO.ITMNO = rec.ITMNO;
						detailVO.ITMNM = rec.ITMNM;
						detailVO.PAYMENT_TYPE = rec.PAYMENT_TYPE;
						detailVO.BETRG = rec.BETRG;
					*/
						vo.paydt = DateUtil.stringToDateTime(rec.PAYDT);
						if(rec.PAYMENT_TYPE == '1') {
							detailVO.ITMNM = rec.ITMNM;
							detailVO.BETRG = Math.round(rec.BETRG*100);
							vo.payDetail.push(detailVO);
						}
						else if(rec.PAYMENT_TYPE == '2') {
							detailVO.ITMNM = rec.ITMNM;
							detailVO.BETRG = Math.round(rec.BETRG*100);
							vo.subtDetail.push(detailVO);
						}
					break;
				}
			}
			var padet:int = vo.payDetail.length;
			var sudet:int = vo.subtDetail.length;
			var dataLeng:int = 0;
			
			if(padet >= sudet) {
				dataLeng = padet;
			}
			else {
				dataLeng = sudet;
			}
			
			for(var i:int = 0; i < dataLeng; i++) {
				var datalVo:PayDetailVO = new PayDetailVO();
				var obj:Object = new Object();
				
				obj.ITMNM = "";
				obj.BETRG = "";
				
				if(sudet < i+1) {
					datalVo.ITMNM = obj.ITMNM;
					datalVo.BETRG_F;
					vo.subtDetail.push(datalVo);
				}
				else if(padet < i+1){
					datalVo.ITMNM = obj.ITMNM;
					datalVo.BETRG_F;
					vo.payDetail.push(datalVo);
				}
			}
		}
	}
}